package com.pai4j.message.config;

import com.pai4j.message.core.builder.AbstractMessageBuilder;
import com.pai4j.message.core.interceptor.InterceptorChain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

/**
 * MessageBuilder Bean后置处理器
 * 自动将InterceptorChain注入到所有MessageBuilder实例中
 *
 * @author PAI4J Team
 * @date 2025-11-01
 */
@Slf4j
@Component
public class MessageBuilderBeanPostProcessor implements BeanPostProcessor {

    @Autowired(required = false)
    private InterceptorChain interceptorChain;

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof AbstractMessageBuilder) {
            if (interceptorChain != null) {
                ((AbstractMessageBuilder<?>) bean).setInterceptorChain(interceptorChain);
                log.debug("注入InterceptorChain到MessageBuilder: {}", beanName);
            } else {
                log.debug("InterceptorChain为null，跳过注入: {}", beanName);
            }
        }
        return bean;
    }
}

